﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/27
 * 
 * 功能说明：功能模块字段的添加或修改页面
 * 
 * 修改人员：
 *      yylu 2009/08/04 增加三级联动判断
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_CommonModel_Field : System.Web.UI.Page
{
    private int modelID;
    private int fieldID;
    private string action;

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        action = NetRequest.GetQueryString("Action");
        modelID = NetRequest.GetQueryInt("ModelID");
        fieldID = NetRequest.GetQueryInt("FieldID");

        if (!IsPostBack)
        {
            txtSQL.Text = "SELECT * FROM ";

            switch (action)
            {
                case "Add":
                    lbFormTitle.Text = "添加字段";
                    ShowPanel(FieldType.TextType);
                    break;
                case "Modify":
                    lbFormTitle.Text = "更新字段";
                    InitControlsProperty();
                    BindData();
                    break;
            }
        }
        this.TxtFieldName.Attributes.Add("style", "ime-mode:disabled;");
    }

    /// <summary>
    /// 根据不同的字段类型设置不同的属性
    /// </summary>
    protected void RadlFieldType_SelectedIndexChanged(object sender, EventArgs e)
    {
        string id = ((RadioButton)sender).ID;
        id = id.Substring(3, id.Length - 3);
        FieldType showType = (FieldType)Enum.Parse(typeof(FieldType), id);
        Type = showType;
        this.HdnFieldType.Value = showType.ToString();
        this.ShowPanel(showType);
    }

    /// <summary>
    /// 字段类型
    /// </summary>
    public FieldType Type
    {
        get
        {
            return ViewState["Type"] == null ? FieldType.TextType : (FieldType)ViewState["Type"];
        }
        set { ViewState["Type"] = value; }
    }

    /// <summary>
    /// 功能模块字段添加或更新操作
    /// </summary>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (FieldCommon.IsSqlKeyWord(Page, TxtFieldName.Text.Trim()))
        {
            JsHelper.Alert(Page, "输入的字段名称为 SQL 关键字，请重新输入！");
            return;
        }
        switch (action)
        {
            case "Add":
                AddField();
                break;
            case "Modify":
                ModifyField();
                Response.Redirect("FieldManage.aspx?ModelID=" + modelID);
                break;
        }

    }

    /// <summary>
    /// 执行更新操作时，初始化控件属性
    /// </summary>
    public void InitControlsProperty()
    {
        RadioRating.Enabled = false;    //级别不能修改
        TxtFieldName.Enabled = false;   //字段名称不能修改

        this.RadTextType.Enabled = false;
        this.RadMultipleTextType.Enabled = false;
        this.RadMultipleHtmlTextType.Enabled = false;
        this.RadListBoxType.Enabled = false;
        this.RadNumberType.Enabled = false;
        this.RadMoneyType.Enabled = false;
        this.RadDateTimeType.Enabled = false;
        this.RadLinkType.Enabled = false;
        this.RadBoolType.Enabled = false;
        this.RadPictureType.Enabled = false;
        this.RadFileType.Enabled = false;
        //this.RadColorType.Enabled = false;
        this.RadNone.Enabled = false;
        this.RadAreaType.Enabled = false;
        this.RadPassWordType.Enabled = false;
    }

    /// <summary>
    /// 执行更新操作时,绑定数据
    /// </summary>
    public void BindData()
    {
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();
        Whir.Model.Whir_Field model = bll.GetModel(fieldID);

        Whir.BLL.Whir_FieldText fieldTextBll = new Whir.BLL.Whir_FieldText();
        Whir.BLL.Whir_FieldEditor fieldEditorBLL = new Whir.BLL.Whir_FieldEditor();
        Whir.BLL.Whir_FieldOption fieldOptionBLL = new Whir.BLL.Whir_FieldOption();
        Whir.BLL.Whir_FieldUpload fieldUploadBLL = new Whir.BLL.Whir_FieldUpload();
        Whir.BLL.Whir_FieldDateText fieldDateTextBLL = new Whir.BLL.Whir_FieldDateText();

        if (model != null)
        {
            TxtFieldName.Text = model.fieldName;
            TxtFieldAliax.Text = model.alias;
            RadioRating.SelectedValue = model.rating.ToString();
            TxtTips.Text = model.fieldTip;
            TxtDescription.Text = model.fieldDesc;
            RadlEnableNull.SelectedValue = model.isRequired.ToString();
            RadIsUse.SelectedValue = model.isUse.ToString();

            switch ((FieldType)model.fieldType)
            {
                case FieldType.BoolType:
                    RadBoolType.Checked = true;
                    PnlBoolean.Visible = true;
                    DropBoolean.SelectedValue = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString(); ;
                    break;
                case FieldType.ColorType:
                    //RadColorType.Checked = true;
                    //PnlColor.Visible = true;
                    //EzColorSelect1.Value = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.DateTimeType:
                    RadDateTimeType.Checked = true;
                    PnlDateTime.Visible = true;
                    RadioDateTimeType.SelectedValue = fieldDateTextBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["dateFormat"].ToString();
                    DateTimeDefaultDate.Text = fieldDateTextBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    if (!string.IsNullOrEmpty(DateTimeDefaultDate.Text) && DateTimeDefaultDate.Text != "1" && DateTimeDefaultDate.Text != "0")
                    { RadlDateTimeDefaultType.SelectedValue = "2"; }
                    else
                    {
                        RadlDateTimeDefaultType.SelectedValue = DateTimeDefaultDate.Text.Trim();
                    }
                    txtDateTimeType.Text = fieldDateTextBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["width"].ToString();
                    break;
                case FieldType.FileType:
                    RadFileType.Checked = true;
                    PnlFile.Visible = true;
                    TxtFileSize.Text = fieldUploadBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["maxSize"].ToString();
                    TextFileType.Text = fieldUploadBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["allowType"].ToString();
                    TxtFileDefaultValue.Text = fieldUploadBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.LinkType:
                    RadLinkType.Checked = true;
                    PnlURL.Visible = true;
                    TxtURLSize.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textWidth"].ToString();
                    TxtURLDefaultValue.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.ListBoxType:

                    RadListBoxType.Checked = true;
                    PnlChoice.Visible = true;

                    string bindType = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["bindType"].ToString();
                    if (bindType == "1")
                    {
                        this.btnSubmit.Attributes.Add("onclick", "if(!ChangeHiddenFieldValue()){return false;}");

                        div1.Visible = true;
                        div2.Visible = false;
                        div3.Visible = false;
                        rbText.Checked = true;
                        string str = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["bindValue"].ToString();
                        foreach (string item in str.Split(','))
                        {
                            ChoiceUrl.Items.Add(item);
                        }
                    }
                    else if (bindType == "2")
                    {
                        div1.Visible = false;
                        div2.Visible = true;
                        div3.Visible = false;
                        rbSql.Checked = true;
                        txtSQL.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["sql"].ToString();
                        txtSQLTextField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textField"].ToString();
                        txtSQLValueField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["valueField"].ToString();
                    }
                    else if (bindType == "3")
                    {
                        div1.Visible = false;
                        div2.Visible = false;
                        div3.Visible = true;
                        rbSql2.Checked = true;
                        RadlChoiceType.Enabled = false;
                        txtSQL2TableName.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["sql"].ToString();
                        txtSQL2ParentID.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["sqlChild"].ToString();
                        txtSQL2TextField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textField"].ToString();
                        txtSQL2ValueField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["valueField"].ToString();
                    }
                    else if (bindType == "4")
                    {
                        div1.Visible = false;
                        div2.Visible = false;
                        div3.Visible = true;
                        rbSql2.Checked = true;
                        RadlChoiceType.Enabled = false;
                        txtSQL2TableName.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["sql"].ToString();
                        txtSQL2ParentID.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["sqlChild"].ToString();
                        txtSQL2TextField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textField"].ToString();
                        txtSQL2ValueField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["valueField"].ToString();
                    }
                    else if (bindType == "5")
                    {
                        div1.Visible = div2.Visible = div3.Visible = false;
                        div4.Visible = !div1.Visible;
                        rbSql3.Checked = true;
                        RadlChoiceType.Enabled = false;
                        txtSQL3TableName.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["sql"].ToString();
                        txtSQL3ParentID.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["sqlChild"].ToString();
                        txtSQL3TextField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textField"].ToString();
                        txtSQL3ValueField.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["valueField"].ToString();
                    }

                    RadlChoiceType.SelectedValue = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["optionType"].ToString();
                    TxtRepeatColumns.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["cellNum"].ToString();
                    TxtChoiceDefaultValue.Text = fieldOptionBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();

                    break;
                case FieldType.MoneyType:
                    RadMoneyType.Checked = true;
                    PnlCurrency.Visible = true;
                    TxtCurrencyDefaultValue.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.MultipleHtmlTextType:
                    RadMultipleHtmlTextType.Checked = true;
                    PnlEditor.Visible = true;
                    DropEditorModel.SelectedValue = fieldEditorBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["editorMode"].ToString();
                    TxtEditorWidth.Text = fieldEditorBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textWidth"].ToString();
                    TxtEditorHight.Text = fieldEditorBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textHeight"].ToString();
                    TxtMulitHtmlDefault.Text = fieldEditorBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    DropEditorType.SelectedValue = fieldEditorBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["editorType"].ToString();
                    break;
                case FieldType.MultipleTextType:
                    RadMultipleTextType.Checked = true;
                    PnlMultiText.Visible = true;
                    TxtMultiTextRow.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textHeight"].ToString();
                    TxtMultiTextWidth.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textWidth"].ToString();
                    TxtMultiDefault.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.NumberType:
                    RadNumberType.Checked = true;
                    PnlNumber.Visible = true;
                    fieldTextBll = new Whir.BLL.Whir_FieldText();
                    TxtNumberDefaultValue.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.PictureType:
                    RadPictureType.Checked = true;
                    PnlImage.Visible = true;
                    TxtImageSize.Text = fieldUploadBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["maxSize"].ToString();
                    TextImageType.Text = fieldUploadBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["allowType"].ToString();
                    TxtPicDefaultUrl.Text = fieldUploadBLL.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.TextType:
                    RadTextType.Checked = true;
                    PnlText.Visible = true;
                    fieldTextBll = new Whir.BLL.Whir_FieldText();
                    TxtTextSize.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textWidth"].ToString();
                    TxtTextDefaultValue.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.None:
                    RadNone.Checked = true;
                    PnlNone.Visible = true;
                    fieldTextBll = new Whir.BLL.Whir_FieldText();
                    txtParam.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();
                    break;
                case FieldType.AreaType:
                    RadAreaType.Checked = true;
                    PnlArea.Visible = true;
                    break;
                case FieldType.PassWordType:
                    RadPassWordType.Checked = true;
                    PnlPassWord.Visible = true;
                    fieldTextBll = new Whir.BLL.Whir_FieldText();
                    TxtTextSize.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["textWidth"].ToString();
                    TxtTextDefaultValue.Text = fieldTextBll.GetList(" FieldID =" + model.fieldID).Tables[0].Rows[0]["defaultValue"].ToString();                    
                    break;
            }
        }
    }

    /// <summary>
    /// 添加字段
    /// </summary>
    public void AddField()
    {
        bool flag = false;
        Whir.Model.Whir_Field model = new Whir.Model.Whir_Field();
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();
        model.modelID = modelID;
        model.fieldName = TxtFieldName.Text;
        model.alias = TxtFieldAliax.Text;
        model.rating = Convert.ToBoolean(RadioRating.SelectedValue);
        model.fieldTip = TxtTips.Text;
        model.fieldDesc = TxtDescription.Text;
        model.isRequired = Convert.ToBoolean(RadlEnableNull.SelectedValue);
        model.isUse = Convert.ToBoolean(RadIsUse.SelectedValue);
        model.sort = 0;
        model.banUserIDs = "";
        model.banRoleIDs = "";
        model.isValidate = true;
        model.validateType = 0;
        model.validateMsg = "";
        model.validateTo = "";
        model.createUserID = 0;
        model.updateUserID = 0;

        FieldType type = Type;
        model.fieldType = (int)Type;
        model.typeName = FieldCommon.GetFieldType(type)["typeName"].ToString();
        model.typeLength = Convert.ToInt32(FieldCommon.GetFieldType(type)["typeLength"]);
        model.isAllowNull = !Convert.ToBoolean(RadlEnableNull.SelectedValue);//Convert.ToBoolean(FieldCommon.GetFieldType(type)["isAllowNull"]);
        model.isIdentity = Convert.ToBoolean(FieldCommon.GetFieldType(type)["isIdentity"]);
        model.identityIncrement = Convert.ToInt32(FieldCommon.GetFieldType(type)["identityIncrement"]);
        model.identitySeed = Convert.ToInt32(FieldCommon.GetFieldType(type)["identitySeed"]);
        model.defaultValue = FieldCommon.GetFieldType(type)["defaultValue"].ToString();

        //添加字段到数据库实际表单中
        Whir.Common.SqlDMOHelper dmo = new Whir.Common.SqlDMOHelper();

        Whir.BLL.Whir_Model b = new Whir.BLL.Whir_Model();
        Whir.Model.Whir_Model m = b.GetModel(modelID);
        string tableName = m.tableName;

        SQLDMO.Column Col1 = new SQLDMO.Column();
        Col1.Name = model.fieldName;
        Col1.Datatype = model.typeName;
        Col1.Length = model.typeLength;
        Col1.AllowNulls = true;
        Col1.Default = model.defaultValue;

        flag = dmo.AddColumnInTable(tableName, Col1);

        if (flag)
        {
            //添加字段到Whir_Field中
            int fieldId = bll.Add(model);

            //添加字段类型
            AddFieldType(fieldId, Type);

            JsHelper.redirect(Page, "字段添加成功！", "FieldManage.aspx?ModelID=" + modelID);
        }
        else
            JsHelper.Alert("字段添加失败，可能该字段在功能模块中已存在！");
    }

    /// <summary>
    /// 更新字段
    /// </summary>
    public void ModifyField()
    {
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();
        Whir.Model.Whir_Field model = bll.GetModel(fieldID);

        model.alias = TxtFieldAliax.Text;
        model.rating = Convert.ToBoolean(RadioRating.SelectedValue);
        model.fieldTip = TxtTips.Text;
        model.fieldDesc = TxtDescription.Text;
        model.isRequired = Convert.ToBoolean(RadlEnableNull.SelectedValue);
        model.isUse = Convert.ToBoolean(RadIsUse.SelectedValue);
        model.isAllowNull = !Convert.ToBoolean(RadlEnableNull.SelectedValue);
        bll.Update(model);

        UpdateFieldType(model.fieldID, (FieldType)model.fieldType);
    }

    /// <summary>
    /// 添加字段类型
    /// </summary>
    /// <param name="fieldID">字段 ID</param>
    /// <param name="fieldType">字段类型</param>
    private void AddFieldType(int fieldID, FieldType fieldType)
    {
        Whir.Model.Whir_FieldText fieldTextModel = new Whir.Model.Whir_FieldText();
        Whir.BLL.Whir_FieldText fieldTextBll = new Whir.BLL.Whir_FieldText();

        Whir.Model.Whir_FieldEditor fieldEditorModel = new Whir.Model.Whir_FieldEditor();
        Whir.BLL.Whir_FieldEditor fieldEditorBLL = new Whir.BLL.Whir_FieldEditor();

        Whir.Model.Whir_FieldOption fieldOptionModel = new Whir.Model.Whir_FieldOption();
        Whir.BLL.Whir_FieldOption fieldOptionBLL = new Whir.BLL.Whir_FieldOption();

        Whir.Model.Whir_FieldUpload fieldUploadModel = new Whir.Model.Whir_FieldUpload();
        Whir.BLL.Whir_FieldUpload fieldUploadBLL = new Whir.BLL.Whir_FieldUpload();

        Whir.Model.Whir_FieldDateText fieldDateTextModel = new Whir.Model.Whir_FieldDateText();
        Whir.BLL.Whir_FieldDateText fieldDateTextBLL = new Whir.BLL.Whir_FieldDateText();

        Whir.Model.Whir_FieldCheckBox fieldCheckBoxModel = new Whir.Model.Whir_FieldCheckBox();
        Whir.BLL.Whir_FieldCheckBox fieldCheckBoxBLL = new Whir.BLL.Whir_FieldCheckBox();
        switch (fieldType)
        {
            case FieldType.None:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";
                fieldTextModel.textType = 1;
                fieldTextModel.defaultValue = txtParam.Text.Trim();
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.TextType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = TxtTextSize.Text;
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtTextDefaultValue.Text.Trim();
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.MultipleTextType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = TxtMultiTextWidth.Text;
                fieldTextModel.textHeight = TxtMultiTextRow.Text;//0为默认
                fieldTextModel.textType = 2; //多行文本
                fieldTextModel.defaultValue = TxtMultiDefault.Text.Trim();
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.MultipleHtmlTextType:
                fieldEditorModel.fieldID = fieldID;
                fieldEditorModel.editorMode = int.Parse(DropEditorModel.SelectedValue);
                fieldEditorModel.textWidth = TxtEditorWidth.Text;
                fieldEditorModel.textHeight = TxtEditorHight.Text;
                fieldEditorModel.defaultValue = TxtMulitHtmlDefault.Text;
                fieldEditorModel.editorType = int.Parse(DropEditorType.SelectedValue);
                fieldEditorBLL.Add(fieldEditorModel);
                break;
            case FieldType.ListBoxType:
                fieldOptionModel.fieldID = fieldID;
                fieldOptionModel.optionType = int.Parse(RadlChoiceType.SelectedValue);    //1、下拉列表框2、单选按钮组3、多选按钮组
                fieldOptionModel.bindValue = HdnChoiceUrls.Value;
                fieldOptionModel.cellNum = int.Parse(TxtRepeatColumns.Text);
                if (rbText.Checked)
                {
                    fieldOptionModel.bindType = 1;
                    fieldOptionModel.sql = "";
                    fieldOptionModel.sqlChild = "";
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.valueField = "";
                    fieldOptionModel.textField = "";
                    fieldOptionModel.defaultValue = TxtChoiceDefaultValue.Text;
                }
                else if (rbSql.Checked)
                {
                    fieldOptionModel.bindType = 2;
                    fieldOptionModel.sql = txtSQL.Text;
                    fieldOptionModel.sqlChild = "";
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.textField = txtSQLTextField.Text.Trim();
                    fieldOptionModel.valueField = txtSQLValueField.Text.Trim();
                    fieldOptionModel.defaultValue = "";
                }
                else if (rbSql2.Checked)
                {
                    fieldOptionModel.bindType = 3;
                    fieldOptionModel.sql = txtSQL2TableName.Text;
                    fieldOptionModel.sqlChild = txtSQL2ParentID.Text;
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.textField = txtSQL2TextField.Text.Trim();
                    fieldOptionModel.valueField = txtSQL2ValueField.Text.Trim();
                    fieldOptionModel.defaultValue = "";
                }
                else if (rbSql3.Checked)
                {
                    fieldOptionModel.bindType = 5;
                    fieldOptionModel.sql = txtSQL3TableName.Text;
                    fieldOptionModel.sqlChild = txtSQL3ParentID.Text;
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.textField = txtSQL3TextField.Text.Trim();
                    fieldOptionModel.valueField = txtSQL3ValueField.Text.Trim();
                    fieldOptionModel.defaultValue = "";
                }
                fieldOptionBLL.Add(fieldOptionModel);
                break;
            case FieldType.NumberType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtNumberDefaultValue.Text;
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.MoneyType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtCurrencyDefaultValue.Text;
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.DateTimeType:
                fieldDateTextModel.fieldID = fieldID;
                fieldDateTextModel.dateFormat = int.Parse(RadioDateTimeType.SelectedValue);
                fieldDateTextModel.defaultValue = RadlDateTimeDefaultType.SelectedValue == "2" ? DateTimeDefaultDate.Text : RadlDateTimeDefaultType.SelectedValue;
                fieldDateTextModel.width = txtDateTimeType.Text;
                fieldDateTextBLL.Add(fieldDateTextModel);
                break;
            case FieldType.LinkType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = TxtURLSize.Text;
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtURLDefaultValue.Text;
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.BoolType:
                fieldOptionModel.fieldID = fieldID;
                fieldOptionModel.optionType = 2;    //1、下拉列表框2、单选按钮组3、多选按钮组
                fieldOptionModel.bindValue = "是|否";
                fieldOptionModel.cellNum = 0;
                fieldOptionModel.defaultValue = DropBoolean.SelectedValue;
                fieldOptionModel.bindType = 1;
                fieldOptionModel.sql = "";
                fieldOptionModel.sqlChild = "";
                fieldOptionModel.height = "";
                fieldOptionModel.width = "";
                fieldOptionModel.firthOption = "";
                fieldOptionModel.onChange = "";
                fieldOptionModel.valueField = "";
                fieldOptionModel.textField = "";
                fieldOptionBLL.Add(fieldOptionModel);
                break;
            case FieldType.PictureType:
                fieldUploadModel.fieldID = fieldID;
                fieldUploadModel.maxSize = int.Parse(TxtImageSize.Text);
                fieldUploadModel.allowType = TextImageType.Text;
                fieldUploadModel.defaultValue = TxtPicDefaultUrl.Text;
                fieldUploadBLL.Add(fieldUploadModel);
                break;
            case FieldType.FileType:
                fieldUploadModel.fieldID = fieldID;
                fieldUploadModel.maxSize = int.Parse(TxtFileSize.Text);
                fieldUploadModel.allowType = TextFileType.Text;
                fieldUploadModel.defaultValue = TxtFileDefaultValue.Text;
                fieldUploadBLL.Add(fieldUploadModel);
                break;
            case FieldType.ColorType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = EzColorSelect1.Value;
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.AreaType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 0;//单行文本; 
                fieldTextModel.defaultValue = "";
                fieldTextBll.Add(fieldTextModel);
                break;
            case FieldType.PassWordType:
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";//TxtTextSize.Text;
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 3; //单行文本
                fieldTextModel.defaultValue = "";// TxtTextDefaultValue.Text.Trim();
                fieldTextBll.Add(fieldTextModel);
                break;
        }
    }

    private void UpdateFieldType(int fieldID, FieldType fieldType)
    {
        Whir.Model.Whir_FieldText fieldTextModel = new Whir.Model.Whir_FieldText();
        Whir.BLL.Whir_FieldText fieldTextBll = new Whir.BLL.Whir_FieldText();

        Whir.Model.Whir_FieldEditor fieldEditorModel = new Whir.Model.Whir_FieldEditor();
        Whir.BLL.Whir_FieldEditor fieldEditorBLL = new Whir.BLL.Whir_FieldEditor();

        Whir.Model.Whir_FieldOption fieldOptionModel = new Whir.Model.Whir_FieldOption();
        Whir.BLL.Whir_FieldOption fieldOptionBLL = new Whir.BLL.Whir_FieldOption();

        Whir.Model.Whir_FieldUpload fieldUploadModel = new Whir.Model.Whir_FieldUpload();
        Whir.BLL.Whir_FieldUpload fieldUploadBLL = new Whir.BLL.Whir_FieldUpload();

        Whir.Model.Whir_FieldDateText fieldDateTextModel = new Whir.Model.Whir_FieldDateText();
        Whir.BLL.Whir_FieldDateText fieldDateTextBLL = new Whir.BLL.Whir_FieldDateText();

        Whir.Model.Whir_FieldCheckBox fieldCheckBoxModel = new Whir.Model.Whir_FieldCheckBox();
        Whir.BLL.Whir_FieldCheckBox fieldCheckBoxBLL = new Whir.BLL.Whir_FieldCheckBox();

        string id = "";
        switch (fieldType)
        {
            case FieldType.None:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = txtParam.Text.Trim();
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.TextType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = TxtTextSize.Text;
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtTextDefaultValue.Text.Trim();
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.MultipleTextType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = TxtMultiTextWidth.Text;
                fieldTextModel.textHeight = TxtMultiTextRow.Text;//0为默认
                fieldTextModel.textType = 2; //多行文本
                fieldTextModel.defaultValue = TxtMultiDefault.Text.Trim();
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.MultipleHtmlTextType:
                id = fieldEditorBLL.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldEditorID"].ToString();
                fieldEditorModel = fieldEditorBLL.GetModel(int.Parse(id));
                fieldEditorModel.fieldID = fieldID;
                fieldEditorModel.editorMode = int.Parse(DropEditorModel.SelectedValue);
                fieldEditorModel.textWidth = TxtEditorWidth.Text;
                fieldEditorModel.textHeight = TxtEditorHight.Text;
                fieldEditorModel.defaultValue = TxtMulitHtmlDefault.Text;
                fieldEditorModel.editorType = int.Parse(DropEditorType.SelectedValue);
                fieldEditorBLL.Update(fieldEditorModel);
                break;
            case FieldType.ListBoxType:
                id = fieldOptionBLL.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldOptionID"].ToString();
                fieldOptionModel = fieldOptionBLL.GetModel(int.Parse(id));
                fieldOptionModel.fieldID = fieldID;
                fieldOptionModel.optionType = int.Parse(RadlChoiceType.SelectedValue);    //1、下拉列表框2、单选按钮组3、多选按钮组
                fieldOptionModel.bindValue = HdnChoiceUrls.Value;
                fieldOptionModel.cellNum = int.Parse(TxtRepeatColumns.Text);
                fieldOptionModel.defaultValue = TxtChoiceDefaultValue.Text;
                if (rbText.Checked)
                {
                    fieldOptionModel.bindType = 1;
                    fieldOptionModel.sql = "";
                    fieldOptionModel.sqlChild = "";
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.valueField = "";
                    fieldOptionModel.textField = "";
                }
                else if (rbSql.Checked)
                {
                    fieldOptionModel.bindType = 2;
                    fieldOptionModel.sql = txtSQL.Text;
                    fieldOptionModel.sqlChild = "";
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.textField = txtSQLTextField.Text.Trim();
                    fieldOptionModel.valueField = txtSQLValueField.Text.Trim();
                }
                else if (rbSql2.Checked)
                {
                    fieldOptionModel.bindType = 3;
                    fieldOptionModel.sql = txtSQL2TableName.Text;
                    fieldOptionModel.sqlChild = txtSQL2ParentID.Text;
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.textField = txtSQL2TextField.Text.Trim();
                    fieldOptionModel.valueField = txtSQL2ValueField.Text.Trim();
                }
                else if (rbSql3.Checked)
                {
                    fieldOptionModel.bindType = 5;
                    fieldOptionModel.sql = txtSQL3TableName.Text;
                    fieldOptionModel.sqlChild = txtSQL3ParentID.Text;
                    fieldOptionModel.height = "";
                    fieldOptionModel.width = "";
                    fieldOptionModel.firthOption = "";
                    fieldOptionModel.onChange = "";
                    fieldOptionModel.textField = txtSQL3TextField.Text.Trim();
                    fieldOptionModel.valueField = txtSQL3ValueField.Text.Trim();
                    fieldOptionModel.defaultValue = "";
                }
                fieldOptionBLL.Update(fieldOptionModel);
                break;
            case FieldType.NumberType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtNumberDefaultValue.Text;
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.MoneyType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtCurrencyDefaultValue.Text;
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.DateTimeType:
                id = fieldDateTextBLL.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldDateTextID"].ToString();
                fieldDateTextModel = fieldDateTextBLL.GetModel(int.Parse(id));
                fieldDateTextModel.fieldID = fieldID;
                fieldDateTextModel.dateFormat = int.Parse(RadioDateTimeType.SelectedValue);
                fieldDateTextModel.defaultValue = RadlDateTimeDefaultType.SelectedValue == "2" ? DateTimeDefaultDate.Text : RadlDateTimeDefaultType.SelectedValue;
                fieldDateTextModel.width = txtDateTimeType.Text;
                fieldDateTextBLL.Update(fieldDateTextModel);
                break;
            case FieldType.LinkType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = TxtURLSize.Text;
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = TxtURLDefaultValue.Text;
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.BoolType:
                id = fieldOptionBLL.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldOptionID"].ToString();
                fieldOptionModel = fieldOptionBLL.GetModel(int.Parse(id));
                fieldOptionModel.fieldID = fieldID;
                fieldOptionModel.optionType = 2;    //1、下拉列表框2、单选按钮组3、多选按钮组
                fieldOptionModel.bindValue = "是|否";
                fieldOptionModel.cellNum = 0;
                fieldOptionModel.defaultValue = DropBoolean.SelectedValue;
                fieldOptionModel.bindType = 1;
                fieldOptionModel.sql = "";
                fieldOptionModel.sqlChild = "";
                fieldOptionModel.height = "";
                fieldOptionModel.width = "";
                fieldOptionModel.firthOption = "";
                fieldOptionModel.onChange = "";
                fieldOptionModel.valueField = "";
                fieldOptionModel.textField = "";
                fieldOptionBLL.Update(fieldOptionModel);
                break;
            case FieldType.PictureType:
                id = fieldUploadBLL.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldUploadID"].ToString();
                fieldUploadModel = fieldUploadBLL.GetModel(int.Parse(id));
                fieldUploadModel.fieldID = fieldID;
                fieldUploadModel.maxSize = int.Parse(TxtImageSize.Text);
                fieldUploadModel.allowType = TextImageType.Text;
                fieldUploadModel.defaultValue = TxtPicDefaultUrl.Text;
                fieldUploadBLL.Update(fieldUploadModel);
                break;
            case FieldType.FileType:
                id = fieldUploadBLL.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldUploadID"].ToString();
                fieldUploadModel = fieldUploadBLL.GetModel(int.Parse(id));
                fieldUploadModel.fieldID = fieldID;
                fieldUploadModel.maxSize = int.Parse(TxtFileSize.Text);
                fieldUploadModel.allowType = TextFileType.Text;
                fieldUploadModel.defaultValue = TxtFileDefaultValue.Text;
                fieldUploadBLL.Update(fieldUploadModel);
                break;
            case FieldType.ColorType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 1; //单行文本
                fieldTextModel.defaultValue = EzColorSelect1.Value;
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.AreaType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextBll.Update(fieldTextModel);
                break;
            case FieldType.PassWordType:
                id = fieldTextBll.GetList(" FieldID=" + fieldID).Tables[0].Rows[0]["fieldTextID"].ToString();
                fieldTextModel = fieldTextBll.GetModel(int.Parse(id));
                fieldTextModel.fieldID = fieldID;
                fieldTextModel.textWidth = "";
                fieldTextModel.textHeight = "";//0为默认
                fieldTextModel.textType = 3; //密码文本
                fieldTextModel.defaultValue = "";//TxtTextDefaultValue.Text.Trim();
                fieldTextBll.Update(fieldTextModel);
                break;
        }
    }

    /// <summary>
    /// 显示不同的类型属性
    /// </summary>
    private void ShowPanel(FieldType showType)
    {
        int position = getPosition(Enum.GetNames(typeof(FieldType)), showType.ToString());
        int length = Enum.GetValues(typeof(FieldType)).Length;
        bool[] flagArray = new bool[length];
        for (int i = 0; i < length; i++)
        {
            if (i == (position - 1))
            {
                flagArray[i] = true;
            }
            else
            {
                flagArray[i] = false;
            }
        }
        if (showType == FieldType.ListBoxType)
        {
            this.btnSubmit.Attributes.Add("onclick", "if(!ChangeHiddenFieldValue()){return false;}");
        }
        else
        {
            this.btnSubmit.Attributes.Add("onclick", "");
        }
        this.PnlText.Visible = flagArray[this.GetEnumIntValue(FieldType.TextType)];
        this.PnlMultiText.Visible = flagArray[this.GetEnumIntValue(FieldType.MultipleTextType)];
        this.PnlEditor.Visible = flagArray[this.GetEnumIntValue(FieldType.MultipleHtmlTextType)];
        this.PnlChoice.Visible = flagArray[this.GetEnumIntValue(FieldType.ListBoxType)];
        this.PnlNumber.Visible = flagArray[this.GetEnumIntValue(FieldType.NumberType)];
        this.PnlCurrency.Visible = flagArray[this.GetEnumIntValue(FieldType.MoneyType)];
        this.PnlDateTime.Visible = flagArray[this.GetEnumIntValue(FieldType.DateTimeType)];
        this.PnlURL.Visible = flagArray[this.GetEnumIntValue(FieldType.LinkType)];
        this.PnlBoolean.Visible = flagArray[this.GetEnumIntValue(FieldType.BoolType)];
        this.PnlImage.Visible = flagArray[this.GetEnumIntValue(FieldType.PictureType)];
        this.PnlFile.Visible = flagArray[this.GetEnumIntValue(FieldType.FileType)];
        this.PnlColor.Visible = flagArray[this.GetEnumIntValue(FieldType.ColorType)];
        this.PnlArea.Visible = flagArray[this.GetEnumIntValue(FieldType.AreaType)];
        this.PnlPassWord.Visible = flagArray[this.GetEnumIntValue(FieldType.PassWordType)];
        if ((FieldType)showType == FieldType.None)
            this.PnlNone.Visible = true;
        else
            this.PnlNone.Visible = false;
    }

    /// <summary>
    /// 获取枚举的对应的值
    /// </summary>
    private int GetEnumIntValue(FieldType fieldType)
    {
        return (((int)fieldType) - 1);
    }

    /// <summary>
    /// 获取指定字符在字符数组中的位置
    /// </summary>
    /// <param name="str">字符数组</param>
    /// <param name="s">指定字符</param>
    /// <returns>位置</returns>
    protected int getPosition(string[] str, string s)
    {
        int temp = 0;
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] == s)
            {
                temp = i;
                break;
            }
        }
        return temp;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("FieldManage.aspx?ModelID=" + modelID);
    }
    protected void rbText_CheckedChanged(object sender, EventArgs e)
    {
        this.btnSubmit.Attributes.Add("onclick", "if(!ChangeHiddenFieldValue()){return false;}");

        div1.Visible = true;
        div2.Visible = false;
        div3.Visible = false;
        RadlChoiceType.Enabled = true;
    }
    protected void rbSql_CheckedChanged(object sender, EventArgs e)
    {
        div1.Visible = false;
        div2.Visible = true;
        div3.Visible = false;
        RadlChoiceType.Enabled = true;
    }
    protected void rbSql2_CheckedChanged(object sender, EventArgs e)
    {
        div1.Visible = false;
        div2.Visible = false;
        div3.Visible = true;
        RadlChoiceType.SelectedIndex = 0;
        //RadlChoiceType.Enabled = false;
    }
    protected void rbSql3_CheckedChanged(object sender, EventArgs e)
    {
        div1.Visible = div2.Visible = div3.Visible = false;
        div4.Visible = true;
    }
}
